package com.party.core.service.commune.impl;

import com.party.common.paging.Page;
import com.party.common.utils.StringUtils;
import com.party.core.dao.read.commune.CMemberInfoReadDao;
import com.party.core.dao.write.commune.CMemberInfoWriteDao;
import com.party.core.model.BaseModel;
import com.party.core.model.commune.CMemberInfo;
import com.party.core.model.member.Member;
import com.party.core.service.commune.ICMemberInfoService;
import com.party.core.service.member.IMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @program: party
 * @description: 积分类型服务实现
 * @author: Hua Qunhao
 * @create: 2019-08-12 14:32
 **/
@Service
public class CMemberInfoService implements ICMemberInfoService {
    @Autowired
    private CMemberInfoReadDao cMemberInfoReadDao;
    @Autowired
    private CMemberInfoWriteDao cMemberInfoWriteDao;
    @Autowired
    private IMemberService memberService;

    /**
     * @param memberInfo
     * @return
     * @Author hzq
     * @Description //修改行知公社 用户信息
     * @Date 11:02 2019/8/13
     **/
    @Override
    @Transactional
    public boolean updateMemberInfo(Member member, CMemberInfo memberInfo) {
        if (StringUtils.isNoneBlank(memberInfo.getMobile())) {
            Member byPhone = memberService.findByPhone(memberInfo.getMobile(), 1);//1  代表个人
            if (null != byPhone && !byPhone.getId().equals(member.getId())) {
                return false;
            }
            member.setMobile(memberInfo.getMobile());
        }
        if (StringUtils.isNoneBlank(memberInfo.getRealname())) {
            member.setRealname(memberInfo.getRealname());
        }
        member.setIndustry(memberInfo.getIndustry());
        member.setBacklogo(member.getLogo());
        member.setLogo(memberInfo.getLogo());
        member.setJobTitle(memberInfo.getJobTitle());
        member.setCompany(memberInfo.getCompany());
        member.setSex(memberInfo.getSex());
        member.setCity(memberInfo.getCity());
        member.setWantRes(memberInfo.getWantRes());
        memberService.update(member);
        CMemberInfo byMemberId = cMemberInfoReadDao.getByMemberId(member.getId());
        if (null == byMemberId) {
            BaseModel.preInsert(memberInfo);
            memberInfo.setMemberId(member.getId());
            cMemberInfoWriteDao.insert(memberInfo);
        } else {
            cMemberInfoWriteDao.update(this.copyCMemberInfo(byMemberId, memberInfo));
        }
        return true;
    }

    /**
     * @param memberId
     * @return
     * @Author hzq
     * @Description //根据用户id 差行知公社信息
     * @Date 15:09 2019/8/13
     **/
    @Override
    public CMemberInfo getByMemberId(String memberId) {
        return cMemberInfoReadDao.getByMemberId(memberId);
    }


    @Override
    public String insert(CMemberInfo cMemberInfo) {
        BaseModel.preInsert(cMemberInfo);
        boolean result = cMemberInfoWriteDao.insert(cMemberInfo);
        if (result) {
            return cMemberInfo.getId();
        }
        return null;
    }

    @Override
    public boolean update(CMemberInfo cMemberInfo) {
        cMemberInfo.setUpdateDate(new Date());
        return cMemberInfoWriteDao.update(cMemberInfo);
    }

    @Override
    public boolean deleteLogic(String id) {
        return false;
    }

    @Override
    public boolean delete(String id) {
        return cMemberInfoWriteDao.delete(id);
    }

    @Override
    public boolean batchDeleteLogic(Set<String> ids) {
        return false;
    }

    @Override
    public boolean batchDelete(Set<String> ids) {
        return false;
    }

    @Override
    public CMemberInfo get(String id) {
        return cMemberInfoReadDao.get(id);
    }

    @Override
    public List<CMemberInfo> listPage(CMemberInfo cMemberInfo, Page page) {
        return null;
    }

    @Override
    public List<CMemberInfo> list(CMemberInfo cMemberInfo) {
        return null;
    }

    @Override
    public List<CMemberInfo> batchList(Set<String> ids, CMemberInfo cMemberInfo, Page page) {
        return null;
    }


    /**
     * @param memberInfo1
     * @param memberInfo2
     * @return
     * @Author hzq
     * @Description // 2对象赋值给1对象
     * @Date 16:26 2019/8/14
     **/
    public CMemberInfo copyCMemberInfo(CMemberInfo memberInfo1, CMemberInfo memberInfo2) {

        memberInfo1.setEmail(memberInfo2.getEmail());


        memberInfo1.setManifesto(memberInfo2.getManifesto());


        memberInfo1.setHobby(memberInfo2.getHobby());


        memberInfo1.setSynopsis(memberInfo2.getSynopsis());


        memberInfo1.setAddress(memberInfo2.getAddress());


        memberInfo1.setDistrict(memberInfo2.getDistrict());

        memberInfo1.setMainBus(memberInfo2.getMainBus());
        memberInfo1.setRecommendUsername(memberInfo2.getRecommendUsername());
        return memberInfo1;
    }

    @Override
    public List<CMemberInfo> queryEffectiveCMembers(Map<String, Object> params) {
        return cMemberInfoReadDao.queryEffectiveCMembers(params);
    }
}
